package com.grace.cms.general.service.search;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.grace.common.config.GraceConfig;
import com.grace.cms.common.domain.entity.CmsTheme;
import com.grace.cms.common.enums.ArticleTypeEnums;
import com.grace.cms.common.enums.ArticleVisibleEnums;
import com.grace.cms.common.enums.properties.OlyWebConfigProperties;
import com.grace.cms.common.model.TimeNum;
import com.grace.cms.common.model.support.PageData;
import com.grace.cms.general.cache.GeneralThemeCacheService;
import com.grace.cms.general.service.IGeneralSearchService;
import com.grace.cms.general.taglib.ConfigTag;
import com.grace.cms.query.mapper.ArticleSearchMapper;
import com.grace.cms.query.model.param.ArticleSearchParam;
import com.grace.cms.query.model.vo.WebArticleVo;

@Service
public class GeneralArticleServiceImpl implements IGeneralSearchService {

    @Autowired
    private ArticleSearchMapper webSearchMapper;

    @Autowired
    private GeneralThemeCacheService themeService;
    
    @Autowired
    private ConfigTag configTag;

    /**
     * 综合查询
     * @param bb
     * @return
     */
    public List<WebArticleVo> listArticleBySearch(ArticleSearchParam bb) {
        return webSearchMapper.listArticleBySearch(bb);
    }

    /**
     * 
     * @param articleId  文章ID
     * @param themeName  主题名称
     * @param useMd      是否使用md
     * @param useContent 是否使用内容
     * @return
     */
    public WebArticleVo selectArticleById(Long articleId, String themeName, Boolean useMd, Boolean useContent) {
        ArticleSearchParam articleSearchParam = getArticleSearchParam(themeName);
        articleSearchParam.setArticleId(articleId);
        articleSearchParam.setUseArticleContent(useContent);
        articleSearchParam.setUseArticleMd(useMd);
        return webSearchMapper.selectArticleById(articleSearchParam);
    }

    /**
     * 完成
     * 
     * @param articleUrl 文章路径
     * @param themeName  主题名字
     * @param useMd      是否使用md
     * @param useContent 是否使用文章内容
     * @return
     */
    public WebArticleVo selectArticleByUrl(String articleUrl, String themeName, Boolean useMd, Boolean useContent) {
        ArticleSearchParam articleSearchParam = getArticleSearchParam(themeName);
        articleSearchParam.setArticleUrl(articleUrl);
        articleSearchParam.setUseArticleContent(useContent);
        articleSearchParam.setUseArticleMd(useMd);
        return webSearchMapper.selectArticleById(articleSearchParam);
    }

    /**
     * 完成
     * 
     * @param themeName
     * @param articleType
     * @param categoryId
     * @param num
     * @param size
     * @param order
     * @return
     */
    public PageData<WebArticleVo> selectPageData(String themeName, Integer articleType, Long categoryId,String crTime, int num,
            int size, String order) {
        ArticleSearchParam ba = getArticleSearchParam(themeName);
        ba.setUseArticleContent(true);
        ba.setUseArticleMd(true);
        ba.setArticleType(articleType);
        ba.setCategoryId(categoryId);
        ba.setCrTime(crTime);
        PageHelper.startPage(num, size, order);
        return PageData.getData(this.listArticleBySearch(ba), 200);
    }

  


    /**
     * 完成
     * 获取主题文章数
     * 
     * @param themeName
     * @param articleTypeEnums
     * @return
     */
    public int selectArticleNum(String themeName, ArticleTypeEnums articleTypeEnums) {
        ArticleSearchParam articleSearchParam = getArticleSearchParam(themeName);
        articleSearchParam.setArticleType(articleTypeEnums.ordinal());
        return webSearchMapper.selectArticleNumUnion(articleSearchParam);
    }

    /**
     * 完成
     * 获取主题文章数
     * 
     * @param themeName
     * @return
     */
    public int selectArticleNum(String themeName) {
        return webSearchMapper.selectArticleNumUnion(getArticleSearchParam(themeName));
    }

    /**
     * 完成
     * 获取文章发布年月列表
     * 
     * @param themeName 主题名
     * @param pageNum   页数
     * @param pageSize  每页大小
     * @return
     */
    public List<TimeNum> listArticleTimeNum(String themeName, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return webSearchMapper.listArticleTimeNum(
                themeService.selectCmsTheme(GraceConfig.getOnlyCode(), themeName).getMainCategoryId());
    }

    public ArticleSearchParam getArticleSearchParam(String themeName) {
        ArticleSearchParam articleSearchParam = new ArticleSearchParam();
        CmsTheme cmsTheme = themeService.selectCmsTheme(GraceConfig.getOnlyCode(), themeName);
        articleSearchParam.setSupportCategoryId(cmsTheme.getMainCategoryId());
        articleSearchParam.setSupportArticleType(cmsTheme.getSupportArticleType());
        articleSearchParam.setQueryCount(Boolean.parseBoolean(configTag.getKeyDefaultEnum(themeName, OlyWebConfigProperties.ARTICLE_QUERY_COUNT)));
        articleSearchParam.setVisible(ArticleVisibleEnums.PASS.ordinal());
        return articleSearchParam;
    }

  
}
